"""
Case Type   : 基础功能
Case Name   : 修改数据库，用户，会话级别的参数idle_in_transaction_session_timeout为空值，合理报错
Create At   : 2022/11/11
Owner       : haomeng802
Description :
    1.查看idle_in_transaction_session_timeout默认值；
    2.修改数据库，用户，会话级别的参数idle_in_transaction_session_timeout为空值
Expect      :
    1.成功
    2.合理报错
History     :
"""

import os
import unittest

from yat.test import Node

from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger


class IdleSession(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        text = f'----{os.path.basename(__file__)} start----'
        self.log.info(text)
        self.sh_primysh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.node = Node(node='PrimaryDbUser')
        self.database_name = self.node.db_name
        self.node_user = self.node.db_user

    def test_idlesession(self):
        step_text1 = "----step1：查看idle_in_transaction_session_timeout默认值；" \
                     "expect：成功----"
        self.log.info(step_text1)
        self.show_sql = 'show idle_in_transaction_session_timeout;'
        self.log.info(self.show_sql)
        result_show = self.sh_primysh.execut_db_sql(self.show_sql)
        self.log.info(result_show)
        self.assertIn('0', result_show, '执行失败' + step_text1)

        step_text2 = "----step2：在gsql中设置数据库、用户、会话级别" \
                     "idle_in_transaction_session_timeout为空值；" \
                     "expect：合理报错----"
        self.log.info(step_text2)
        alter_sql = f"alter database {self.database_name} set " \
                    f"idle_in_transaction_session_timeout to 'test';" \
                    f"alter user {self.node_user} set " \
                    f"idle_in_transaction_session_timeout to 'test';" \
                    f"set idle_in_transaction_session_timeout to 'test';"
        result_sql = self.sh_primysh.execut_db_sql(alter_sql)
        self.log.info(result_sql)
        self.assertEqual(3, result_sql.count('ERROR'), "执行失败" + step_text2)

    def tearDown(self):
        self.log.info(f'----{os.path.basename(__file__)} end----')
